package com.qq.vip.mikuPy2001.bukkit.gui.util;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

/**
 *
 * @author MukiPy2001
 */
public class ItemUtil {

    static public ItemStack newItem(Material type, String name, String... lore) {
        return newItem(type, 1, (short) 0, name, lore);
    }

    static public ItemStack newItem(Material type, int amount, String name, String... lore) {
        return newItem(type, amount, (short) 0, name, lore);
    }

    static public ItemStack newItem(Material type, short damage, String name, String... lore) {
        return newItem(type, 1, damage, name, lore);
    }

    static public ItemStack newItem(Material type, int amount, short damage, String name, String... lore) {
        amount = amount < 1 ? 1 : amount;
        damage = damage < 0 ? 0 : damage;
        ItemStack itemStack = new ItemStack(type, amount, damage);
        if (!(name == null || name.isEmpty()) || lore.length > 0) {
            ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(type);
            if (!(name == null || name.isEmpty()))
                itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
            if (lore.length > 0) {
                for (int i = 0; i < lore.length; i++)
                    lore[i] = ChatColor.translateAlternateColorCodes('&', lore[i]);
                itemMeta.setLore(Arrays.asList(lore));
            }
            itemStack.setItemMeta(itemMeta);
        }
        return itemStack;
    }

    static public ItemStack loadFromConfiguration(ConfigurationSection con) {
        if (con == null)
            throw new RuntimeException("Configuration is null");

        ItemStack is;
        Material material;
        int amount = 1;

        if (!con.contains("ID"))
            throw new RuntimeException("ID not found in Configuration");

        // 获取 物品类型
        String materialstr = con.getString("ID");
        String[] split = materialstr.split(":");
        // 获取 Material
        try {
            material = Material.getMaterial(Integer.valueOf(split[0]));
        } catch (Exception e1) {
            try {
                material = Material.getMaterial(split[0]);
            } catch (Exception e2) {
                throw new RuntimeException(materialstr + " is not a ItemStack");
            }
        }

        // 获取数量
        if (con.isInt("amount"))
            amount = con.getInt("amount");
        amount = amount < 1 ? 1 : amount;
        // 获取物品
        if (split.length == 2)
            try {
                is = new ItemStack(material, amount, Short.valueOf(split[1]));
            } catch (Exception e) {
                is = new ItemStack(material, amount);
            }
        else
            is = new ItemStack(material);

        // 设置 amount(数量) 和 durability(耐久)
        if (con.isInt("durability"))
            is.setDurability((short) con.getInt("durability"));
        loadItemMeta(con, is);

        return is;
    }

    private static void loadItemMeta(ConfigurationSection con, ItemStack is) {
        ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(is.getType());
        boolean set = false;
        if (con.isString("name")) {
            itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', con.getString("name")));
            set = true;
        }
        if (con.isList("lore")) {
            List<String> lorelist = con.getStringList("lore");
            for (int i = 0; i < lorelist.size(); i++)
                lorelist.set(i, ChatColor.translateAlternateColorCodes('&', lorelist.get(i)));
            itemMeta.setLore(lorelist);
            set = true;
        }
        if (con.isConfigurationSection("ench")) {
            ConfigurationSection enchcon = con.getConfigurationSection("ench");
            Set<String> enchs = enchcon.getKeys(false);
            for (String ench : enchs)
                if (enchcon.isInt(ench)) {
                    Enchantment enchantment = Enchantment.getByName(ench);
                    if (enchantment == null)
                        continue;
                    int lv = enchcon.getInt(ench);
                    itemMeta.addEnchant(enchantment, lv, true);
                    set = true;
                }
        }
        if (set)
            is.setItemMeta(itemMeta);
    }

    static public String toString(ItemStack is) {
        StringBuilder sb = new StringBuilder("ItemStack:[");
        if (isEmpty(is))
            return sb.append("Air or null]").toString();
        Material type = is.getType();
        sb.append("Type: ").append(type.name()).append(", ");
        List<String> lore = null;
        if (is.hasItemMeta()) {
            ItemMeta it = is.getItemMeta();
            if (it.hasDisplayName())
                sb.append("名称: ").append(it.getDisplayName()).append(ChatColor.RESET).append(", ");
            if (it.hasLore())
                lore = it.getLore();
        }
        if (is.getAmount() > 1)
            sb.append("数量: ").append(is.getAmount()).append(", ");
        if (is.getDurability() > 1)
            sb.append("耐久: ").append(is.getDurability()).append(", ");
        if (lore != null) {
            String[] list = lore.toArray(new String[0]);
            for (int i = 0; i < list.length; i++)
                list[i] = list[i] + ChatColor.RESET;
            sb.append("lore: ").append(Arrays.toString(list)).append(", ");
        }
        return sb.substring(0, sb.length() - 2) + " ]";
    }

    static public boolean isEmpty(ItemStack is) {
        return is == null || is.getType() == Material.AIR;
    }

    static public boolean equals(ItemStack is1, ItemStack is2) {
        return isEmpty(is1) ? isEmpty(is2) : !isEmpty(is2) && (is1 == is2 || is1.equals(is2));
        //return isEmpty(is1) ? isEmpty(is2) : isEmpty(is2) ? false : is1 == is2 ? true : is1.equals(is2);
    }
}
